layui.use(['form', 'table','layuimini','element'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        layuimini = layui.layuimini;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    var MapDistrict = {
        id: 'MapDistrictTable'
    };

    var column = [
        [
            {type: 'numbers', title: '序号'},
            {field: 'name', title: '名称', align: 'center'},
            {field: 'longName', title: '省市县', align: 'center'},
            {field: 'adcode', title: '编码', align: 'center'},
            {field: 'paAdcode', title: '上级编码', align: 'center'},
            {field: 'level', title: '级别', align: 'center',
                templet: function (object) {
                    if('province' == object.level){
                        return '<span class="layui-badge layui-bg-green">省/直辖市</span>';
                    }else if('city' == object.level){
                        return '<span class="layui-badge layui-bg-blue">城市</span>';
                    }else if('district' == object.level){
                        return '<span class="layui-badge layui-bg-red">区/县</span>';
                    }else if('street' == object.level){
                        return '<span class="layui-badge layui-bg-cyan">街道</span>';
                    }
                }
            },
            {field: 'center', title: '位置坐标', align: 'center'},
            {field: 'updateDate', title: '更新时间', minWidth: 120, align: 'center'},
            // {field: 'updateby', title: '更新者', minWidth: 120, align: 'center'},
            // {field: 'createDate', title: '创建时间', minWidth: 120, align: 'center'},
            // {templet: '#stateBtn', width: 140, align: 'center', title: '操作'}
        ]
    ];

    table.render({
        elem: '#' + MapDistrict.id,
        url: '/map/mapDistrict/list',
        toolbar: '#toolbarBtn',
        defaultToolbar: ['filter', 'exports', 'print'],
        cols: column,
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        page: true
    });

    // 监听搜索操作
    $("#data-search-btn").on('click', function () {

        //获取搜索条件
        var name = $("#search_name").val();
        var longName = $("#search_longName").val();
        var adcode = $("#search_adcode").val();
        var paAdcode = $("#search_paAdcode").val();
        var level = $("#search_level").val();

        //封装
        var dataObject = {};
        dataObject['name'] = name;
        dataObject['longName'] = longName;
        dataObject['adcode'] = adcode;
        dataObject['paAdcode'] = paAdcode;
        dataObject['level'] = level;

        var result = JSON.stringify(dataObject);

        //执行搜索重载
        table.reload(MapDistrict.id, {
            page: {
                curr: 1
            }
            , where: {
                searchParams: result
            }
        }, 'data');

        return false;
    });

    // 监听重置操作
    $("#data-reset-btn").on('click', function () {

        //初始化
        $("#search_name").val("");
        $("#search_longName").val("");
        $("#search_adcode").val("");
        $("#search_paAdcode").val("");
        $("#search_level").val("-1");

        form.render();

        //封装
        var dataObject = {};
        dataObject['name'] = "";
        dataObject['longName'] = "";
        dataObject['adcode'] = "";
        dataObject['paAdcode'] = "";
        dataObject['level'] = "-1";
        var result = JSON.stringify(dataObject);

        //执行搜索重载
        table.reload(MapDistrict.id, {
            page: {
                curr: 1
            }
            , where: {
                searchParams: result
            }
        }, 'data');

        return false;
    });

    // 监听生成行政地区操作
    $(".layuimini-container").on("click", "#get-district-btn", function () {
        layer.open({
            title: '生成行政地区'
            ,content: '是否确定生成行政地区？'
            ,icon: 3
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
                layer.close(index);
                var indexLoad = layer.load(2);
                var url = '/map/mapDistrict/withUpdateDistrict';
                $.post(url, {}, function(data){
                    layer.close(indexLoad);
                    if(data.state == "200"){
                        layer.msg(data.message, {icon: 1});
                        table.reload(MapDistrict.id);
                    }else{
                        layer.msg(data.message, {icon: 2});
                    }
                })
            }
        });

        return true;
    });

    /**
     * 点击删除按钮
     * @param id
     */
    MapDistrict.delete = function(id){
        layer.open({
            title: '删除行政地区'
            ,content: '是否确定删除该行政地区？'
            ,icon: 3
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
                layer.close(index);
                var indexLoad = layer.load(2);
                var url = '/map/mapDistrict/delete';
                $.post(url, {id: id}, function(data){
                    layer.close(indexLoad);
                    if(data.state == "200"){
                        layer.msg(data.message, {icon: 1});
                        table.reload(MapDistrict.id);
                    }else{
                        layer.msg(data.message, {icon: 2});
                    }
                })
            }
        });

        return false;
    };


    //监听表格复选框选择
    // table.on('checkbox(currentTableFilter)', function (obj) {
    //     console.log(obj)
    // });

    //监听工具条
    table.on('tool(' + MapDistrict.id + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'delete') {
            MapDistrict.delete(data.id);
        }
    });

});